package short_semester.questionnaire_planet.service.editService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.dao.ScoreOptionDao;
import short_semester.questionnaire_planet.dao.ScoreQuestionDao;
import short_semester.questionnaire_planet.dao.ScoreQuestionRecordDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.editRequest.EditScoreOptionBody;
import short_semester.questionnaire_planet.request.editRequest.EditScoreQuestionBody;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.response.editResponse.RelationResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ScoreQuestionService {
    
    @Autowired
    ScoreQuestionDao questionDao;
    
    @Autowired
    ScoreOptionDao optionDao;
    
    @Autowired
    QuestionSequenceDao sequenceDao;

    @Autowired
    ScoreQuestionRecordDao recordDao;
    
    @Autowired
    RelationService relationService;
    
    public List<ScoreQuestion> getQuestions(Questionnaire questionnaire){
        List<ScoreQuestion> questions = questionDao.findByQuestionnaire(questionnaire);
        
        for(ScoreQuestion question :questions){
            question.setOptions(optionDao.findByScoreQuestionOrderBySequenceAsc(question));
        }
        
        return questions;
    }
    
    public ScoreQuestion getQuestion(Integer questionId){
        ScoreQuestion question = questionDao.getById(questionId);
        question.setOptions(optionDao.findByScoreQuestionOrderBySequenceAsc(question));
        return question;
    }

    public ScoreQuestion getQuestion(ScoreQuestion question){
        question.setOptions(optionDao.findByScoreQuestionOrderBySequenceAsc(question));
        return question;
    }
    
    public List<ScoreQuestion> getQuestionInfo(Questionnaire questionnaire){
        return questionDao.findByQuestionnaire(questionnaire);
    }
    
    public CommonResponse getQuestionResponse(Integer id){
        CommonResponse response = new CommonResponse();
        
        try {
            ScoreQuestion question = questionDao.getById(id);
            question.setOptions(optionDao.findByScoreQuestionOrderBySequenceAsc(question));
            response.setData(question);
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    public int getQuestionNum(Questionnaire questionnaire){
        return questionDao.countByQuestionnaire(questionnaire);
    }

    public int addQuestion(Questionnaire questionnaire,Integer sequence,String qtype){
        ScoreQuestion question = new ScoreQuestion(qtype);
        question.setQuestionnaire(questionnaire);
        question.setSequence(sequence);
        question = questionDao.save(question);
        

        QuestionSequence questionSequence = new QuestionSequence();
        questionSequence.setQuestionnaire(questionnaire);
        questionSequence.setSequence(sequence);
        questionSequence.setType(question.getType());
        questionSequence.setQuestionId(question.getId());
        sequenceDao.save(questionSequence);
        return question.getId();
    }
    
    public void deleteQuestion(Integer questionId){
        ScoreQuestion question = questionDao.findById(questionId).get();
//        for (ScoreOption option: optionDao.findByScoreQuestion(question)){
//            optionDao.delete(option);
//        }
        optionDao.deleteByScoreQuestion(question);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), question.getId());
        sequenceDao.delete(questionSequence);

        relationService.relationDao.deleteByQuestionIdAndType(questionId,question.getType());
//        relationService.relationDao.deleteByRelationIdAndRelationType(questionId,question.getType());
        
        questionDao.delete(question);
    }
    
    public ScoreQuestion getQuestionById(Integer questionId){
        return questionDao.getById(questionId);
    }
    
    public void setSequenceById(Integer questionId, Integer sequence){
        ScoreQuestion question = questionDao.getById(questionId);
        question.setSequence(sequence);
        questionDao.save(question);

        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), questionId);
        questionSequence.setSequence(sequence);
        sequenceDao.save(questionSequence);
    }
    
    public CommonResponse editQuestion(EditScoreQuestionBody body){
        CommonResponse response = new CommonResponse();
        
        try {
            ScoreQuestion question = questionDao.getById(body.getId());
            question.setValueFrom(body);
            questionDao.save(question);
            
            List<ScoreOption> options = optionDao.findByScoreQuestion(question);
            Map<Integer, ScoreOption> optionMap = new HashMap<>();
            for (ScoreOption option: options){
                optionMap.put(option.getId(),option);
            }
            
            int sequence = 0;
            for(EditScoreOptionBody optionBody: body.getOptions()){
                ScoreOption option = new ScoreOption(optionBody);
                option.setScoreQuestion(question);
                option.setSequence(sequence);
                ++sequence;

                optionMap.remove(option.getId());
                
                optionDao.save(option);
            }
            
            for(ScoreOption option: optionMap.values()){
                optionDao.delete(option);
            }
            
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    public void copyQuestion(Questionnaire questionnaire,Questionnaire oldQuestionnaire){
        for (ScoreQuestion question : questionDao.findByQuestionnaire(oldQuestionnaire)) {
            ScoreQuestion scoreQuestion = new ScoreQuestion(question);
            scoreQuestion.setQuestionnaire(questionnaire);
            scoreQuestion = questionDao.save(scoreQuestion);

            QuestionSequence questionSequence = new QuestionSequence(scoreQuestion);
            sequenceDao.save(questionSequence);

            for (ScoreOption option : optionDao.findByScoreQuestion(question)) {
                ScoreOption scoreOption = new ScoreOption(option);
                scoreOption.setScoreQuestion(scoreQuestion);
                scoreOption = optionDao.save(scoreOption);

            }
        }
    }

    //彻底删除题目，包括选项与答题
    public void completeDelete(ScoreQuestion scoreQuestion){
        recordDao.deleteByScoreQuestion(scoreQuestion);
        optionDao.deleteByScoreQuestion(scoreQuestion);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(scoreQuestion.getType(), scoreQuestion.getId());
        sequenceDao.delete(questionSequence);


        questionDao.delete(scoreQuestion);
    }

    //删除题目对应的回答
    public void deleteAnswer(ScoreQuestion scoreQuestion){
        recordDao.deleteByScoreQuestion(scoreQuestion);
    }

    public RelationResponse getRelation(Integer questionId){
        ScoreQuestion scoreQuestion = questionDao.getById(questionId);
        return relationService.getRelation(scoreQuestion);
    }
}
